package com.mazong.servershiroaa.shiro;

import com.mazong.servershiroaa.service.ShiroService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.Objects;

@Slf4j
public class MyCredentialsMatcher implements CredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

        //--1 获取用户的输入
        UsernamePasswordToken utoken = (UsernamePasswordToken) token;
        String inPassword = new String(utoken.getPassword());
        String username = utoken.getUsername();
        log.info("inPassword={}",inPassword);

        //--2 获得数据库中的密码
        String dbPassword = (String) info.getCredentials();
        String md5Password = DigestUtils.md5DigestAsHex(inPassword.getBytes());

        //--3 比对密码
        boolean flag = Objects.equals(md5Password, dbPassword);
        log.info("dbpass={},inpass={}", dbPassword, md5Password);

        //--4 返回对比结果
        return flag;
    }
}
